package indi.zhifa.expshare.class001.chemicalmatch.controller;

import indi.zhifa.expshare.class001.chemicalmatch.entity.dto.NickNameDto;
import indi.zhifa.expshare.class001.chemicalmatch.entity.dto.NickNameFindRequest;
import indi.zhifa.expshare.class001.chemicalmatch.entity.dto.NickNameInfoRequest;
import indi.zhifa.expshare.class001.chemicalmatch.entity.dto.NickNamePageRequest;
import indi.zhifa.expshare.class001.chemicalmatch.entity.po.NickNameMongoData;
import indi.zhifa.expshare.class001.chemicalmatch.entity.vo.ProductTypeVo;
import indi.zhifa.expshare.class001.chemicalmatch.service.INickNameService;
import indi.zhifa.engine.core.common.entity.web.RestResponse;
import indi.zhifa.engine.core.common.web.ann.ZhiFaRestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@RequiredArgsConstructor
@RequestMapping(value = "api/nickName")
@ZhiFaRestController
@Tag(name = "昵称")
public class NickNameController {

    private final INickNameService nickNameService;

    @Operation(summary = "导入")
    @PostMapping("/import")
    public RestResponse<String> importChemicalNickNameFromExcel(@RequestPart MultipartFile pFileRequest) throws IOException{
        nickNameService.importChemicalNickNameFromExcel(pFileRequest);
        return RestResponse.ok("导入成功");
    }

    @Operation(summary = "info查询")
    @GetMapping("/info/{id}")
    public NickNameMongoData info(@PathVariable("id") String pId){
        return nickNameService.info(pId);
    }

    @Operation(summary = "info查询，根据名称")
    @PostMapping("/info")
    public NickNameMongoData info(@RequestBody NickNameInfoRequest pNickNameInfoRequest){
        return nickNameService.info(pNickNameInfoRequest);
    }

    @Operation(summary = "添加或创建昵称")
    @PostMapping("/add")
    public RestResponse<String> addNickName(@Validated @RequestBody NickNameDto pNickNameDto) {
        nickNameService.addNickName(pNickNameDto);
        return RestResponse.ok("添加成功");
    }

    @Operation(summary = "添加昵称")
    @PostMapping("/add/{id}")
    public RestResponse<String> addNickName(@PathVariable("id") String pId,
                                            @RequestBody List<String> pNickNames) {
        nickNameService.addNickName(pId,pNickNames);
        return RestResponse.ok("添加成功");
    }



    @Operation(summary = "删除昵称",description = "nickName不填，则删除整个数据")
    @DeleteMapping("/{id}")
    public RestResponse<String> deleteNickName(@PathVariable("id") String pId, @RequestParam("nickName") String pNickName){
        nickNameService.delete(pId,pNickName);
        return RestResponse.ok("删除成功");
    }

    @Operation(summary = "查找昵称" ,description = "根据昵称查原名")
    @PostMapping("/findOne")
    public NickNameMongoData findNickname(@RequestBody NickNameFindRequest pNickNameFindRequest){
        NickNameMongoData rtn = nickNameService.findNickname(pNickNameFindRequest);
        return rtn;
    }

    @Operation(summary = "获取产品类型列表")
    @GetMapping("/productTypeList")
    public List<ProductTypeVo> getProductTypeList(){
        return nickNameService.getProductTypeList();
    }

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    public Map<String, Page<NickNameMongoData>> page(@RequestBody List<NickNamePageRequest> pRequestList){
        return nickNameService.page(pRequestList);
    }

    @Operation(summary = "清空")
    @DeleteMapping("/clear")
    public RestResponse<String> clear(){
        nickNameService.clear();
        return RestResponse.ok("清空成功");
    }

}
